package net.fredstar.polarbear;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import javax.net.ssl.SSLSocket;

/**
 * TODO Comments.
 * 
 * @author Matthew Blackford [matthew@fredstar.net]
 */
public class SocketHandler extends Thread
{
  private final SSLSocket socket;

  /**
   * Creates a new {@link SocketHandler} instance.
   * 
   * @param socket
   */
  public SocketHandler( final SSLSocket socket )
  {
    this.socket = socket;
  }
  
  /**
   * {@inheritDoc}
   */
  @Override
  public void run()
  {
    try
    {
      InputStreamReader reader = new InputStreamReader( socket.getInputStream() );
      BufferedReader in = new BufferedReader( reader );
      
      System.out.println( "Waiting for line" );
      String line = in.readLine();
      System.out.println( "Read first line: " + line );
      while ( line != null )
      {
        System.out.println( "Read line: " + line );
        line = in.readLine();
      }
    }
    catch ( Exception ex )
    {
      ex.printStackTrace();
    }
    finally
    {
      try
      {
        socket.close();
      }
      catch ( IOException e )
      {
      }
    }
  }
}
